/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 */
package com.hyyt.shopjsp.wxmg.sendMessage.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.wxmg.sendMessage.pojo.WxPublicNumberSendMsgInfo;
import com.hyyt.shopjsp.wxmg.sendMessage.service.IWxPublicNumberSendMsgInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * wx_public_number_send_msg_info, WX_公众号发送消息信息
 *
 * @author Liu Ling on 2017/12/25
 */

@Controller
@RequestMapping("/back/wx/publicNumberSendMsgInfo")
public class WxPublicNumberSendMsgInfoController extends BaseController {

    private Logger log = Logger.getLogger(WxPublicNumberSendMsgInfoController.class);
    private String basePath = "/back/wxmg/sendMessage/";

    @Resource
    private IWxPublicNumberSendMsgInfoService wxPublicNumberSendMsgInfoService;

    /**
     * 进入公众号发送消息页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoPublicNumberSendMsgInfoPage")
    public String gotoPublicNumberSendMsgInfoPage() {
        return basePath + "sendMessageList";
    }

    /**
     * 查询公众号发送信息
     * @param sendBeginTime 发送开始时间
     * @param sendEndTime 发送结束时间
     * @param sendMsgTypeEnumId 发送消息类型
     * @param page 分页
     * @param response 返回结果集
     */
    @RequestMapping(value="listSendMessage",method = RequestMethod.POST)
    @ResponseBody
    public void listSendMessage(String sendBeginTime,String sendEndTime,String sendMsgTypeEnumId,Page page, HttpServletResponse response){
        Map<String,Object> params=new HashMap<>();
        if(Utils.objectIsNotEmpty(sendBeginTime)){
            params.put("sendBeginTime",sendBeginTime);
        }
        if(Utils.objectIsNotEmpty(sendEndTime)){
            params.put("sendEndTime",sendEndTime);
        }
        if(Utils.objectIsNotEmpty(sendMsgTypeEnumId)){
            params.put("sendMsgTypeEnumId",sendMsgTypeEnumId);
        }
        int totalRecordCount = wxPublicNumberSendMsgInfoService.countWxPublicNumberSendMsgInfo(params);
        params.put("sortColumns","sendMsgInfoId desc");
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<WxPublicNumberSendMsgInfo> wxPublicNumberSendMsgInfo= wxPublicNumberSendMsgInfoService.queryWxPublicNumberSendMsgInfo(params,page.getPageRecordBeginIndex(), page.getPageSize());
        params.clear();
        params.put("total", totalRecordCount);// total键 存放总记录数，必须的
        params.put("rows", wxPublicNumberSendMsgInfo);// rows键 存放每页记录 list
        ResultMsg.create(params, response);  //把内容发送到页面

    }

    /**
     * 获得公众号发送消息对象
     * @param sendMsgInfoId 发送消息信息ID
     * @return 返回布尔值
     */
    @RequestMapping(value = "getSendMessageInfoObject",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getSendMessageInfoObject (Integer sendMsgInfoId){
        if(Utils.objectIsNotEmpty(sendMsgInfoId)){
            WxPublicNumberSendMsgInfo wxPublicNumberSendMsgInfo = wxPublicNumberSendMsgInfoService.loadWxPublicNumberSendMsgInfo(sendMsgInfoId);
            if(Utils.objectIsNotEmpty(wxPublicNumberSendMsgInfo)){
                return ResultMsg.create(true, null, FastJsonUtils.toJSONString(wxPublicNumberSendMsgInfo));
            }else{
                return  ResultMsg.create(false);
            }
        }else{
            return  ResultMsg.create(false);
        }
    }

    /**
     * 删除公众号发送消息信息
     * @param sendMsgInfoIds 发送消息信息ID组
     * @return 返回布尔值
     */
    @RequestMapping(value = "deleteSendMessageInfo")
    @ResponseBody
    public ResultMsg deleteSendMessageInfo(String sendMsgInfoIds){
        boolean flag=false;
        String [] sendMsgInfoId = sendMsgInfoIds.split(",");
        for (int i=0; i< sendMsgInfoId.length;i++) {
            if(Utils.objectIsNotEmpty(sendMsgInfoId[i])){
                wxPublicNumberSendMsgInfoService.deleteWxPublicNumberSendMsgInfo(Integer.parseInt(sendMsgInfoId[i]));
                flag=true;
            }
        }
        return ResultMsg.create(flag);
    }

}